package br.usp.wta.inductor;

import static org.junit.Assert.*;

import org.junit.Before;
import org.junit.Test;

import br.usp.language.automata.State;
import br.usp.language.automata.StateMachine;
import br.usp.language.syntax.grammar.ContextFreeGrammar;
import br.usp.language.syntax.grammar.ProductionElement;
import br.usp.language.syntax.grammar.ProductionRule;

public class IvoneTest3 {

    StateMachine fsm;
    State s0;
    State s1;
    State s2;
    State s3;
    State s4;
    State s5;
    Ivone ivone;

    @Before
    public void setUp() throws Exception {
        // Maquina de teste:
        
        fsm = new StateMachine("TesteArtigoIvone");
        s0 = fsm.getInitialState();
        s1 = fsm.createState("S1");
        s2 = fsm.createState("S2");
        s3 = fsm.createState("S3");
        s3.setAcceptState();
        s4 = fsm.createState("S4");
        s5 = fsm.createState("S5");
        
        s0.createTransitionTo(s1, "a", null);
        s1.createTransitionTo(s3, "b", null);
        s1.createTransitionTo(s2, "a", null);
        s2.createTransitionTo(s1, "b", null);
        s2.createTransitionTo(s4, "a", null);
        s4.createTransitionTo(s2, "b", null);
        s4.createTransitionTo(s5, "a", null);
        s5.createTransitionTo(s4, "b", null);
        
        ivone = new Ivone();
    }
    
    @Test
    public void test() {
        ContextFreeGrammar grammar = ivone.inferAPE(fsm);
        assertEquals(3, grammar.getRules().size());
        for (ProductionRule rule : grammar.getRules()) {
            if (rule.getGenerator().getName().equals("S0")) {
                assertEquals(3, rule.getProducts().size());
                assertEquals("a", rule.getProducts().get(0).getName());
                assertEquals(ProductionElement.NONTERMINAL, rule.getProducts().get(1).getType());
                assertEquals("b", rule.getProducts().get(2).getName());
            } else {
                assertEquals("S1", rule.getGenerator().getName());
            }
        }
    }
}
